package cn.wolfcode.redis.util;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * @Description: 优化redis的keys的工具类
 * @Author: 伍家钰
 * @Date: 2020/8/8 20:28
 * @Version: 1.0
 */
@Getter
@NoArgsConstructor
public enum RedisKeysUtil {
    //短信验证码
    VERIFY_CODE("verify_code", 60 * 5L),
    //登录token
    LOGIN_TOKEN("user_login_token", 60 * 30L);

    /**
     * keys的前缀
     */
    @Setter
    private String prefix;

    /**
     * keys的超时时间
     */
    @Setter
    private Long time;

    private RedisKeysUtil(String prefix, Long time) {
        this.prefix = prefix;
        this.time = time;
    }

    public String join(String... keys) {
        StringBuilder sb = new StringBuilder(80);
        // 使用枚举类的实例时(用的有参构造器创建的)就就已经给prefix设置了值等于verify_code
        sb.append(prefix);
        for (String key : keys) {
            sb.append(":").append(key);
        }
        return sb.toString();
    }
}
